/**
 * @author wuqh
 */
function doAjax(postUrl,postData,successFunc,errorFunc,beforeSendFunc,completeFunc) {
	jQuery.ajax({
		type: "POST",
		global : false,
		contentType: "application/x-www-form-urlencoded;charset=UTF-8",
		url: postUrl,
		data: postData,
		beforeSend: beforeSendFunc,
		success: successFunc,
		error: errorFunc,
		complete: completeFunc
	});
}

var Pagination = function(args) {
	this.arcCur = args.arcCur;
    this.START = 1;
    this.MIDDLE = 2;
    this.END = 3;
    this.NONE = 0;
    this.viewType = this.NONE;
    this.curClass = args.curClassStr;
    this.curStyle = args.curStyleStr;
    this.norClass = args.norClassStr;
    this.norStyle = args.norStyleStr;
    this.decStr = args.decStr;
	this.maxSize = args.maxSize;
	this.pageObj = args.pageObj;
	
    this._getIndexAarry = function(page, maxSize){
        var total = page.totalPage;
        var current = page.curpage;
        if (total <= maxSize) {
            this.viewType = this.NONE;
            var index = [];
            for (var i = 0; i < total; i++)
                index[i] = i + 1;
            return index;
        }        
        var middle = Math.floor((maxSize + 1) / 2);
        if (current <= middle) {
            this.viewType = this.START;
            var index = [];
            for (var i = 0; i < maxSize; i++)
                index[i] = i + 1;
            return index;
        }
        middle = Math.floor(maxSize / 2);
        if ((current + middle) >= total) {
            this.viewType = this.END;
            var index = [];
            for (var i = 0, value = (total - maxSize + 1); i < maxSize; i++, value++)
                index[i] = value;
            return index;
        }
        this.viewType = this.MIDDLE;
        middle = Math.floor((maxSize + 1) / 2);
        var index = [];
        var startIndex = current - middle + 1;
        for (var i = 0; i < maxSize; i++, startIndex++)
            index[i] = startIndex;
        return index;
    }
	
    this._generateLinkStr = function(arcCur, curClass, curStyle, curpage, decStr){
        var dec = curpage;
        if(decStr.replace(/(^\s*)|(\s*$)/g, "") != "")
        	dec = decStr;
        if (decStr.indexOf("%s") != -1) 
            dec = decStr.replace("%s", curpage);
        var arcStrArr = [];
        if(!this.pageObj) {
        	this.pageObj = "p";
        }
        if (arcCur) 
            arcStrArr.push('<a href="javascript:' + this.pageObj + '.jumpTo(' + curpage + ');"');        
        else 
            arcStrArr.push('<span');        
        arcStrArr.push(' class="');
        arcStrArr.push(curClass);
        arcStrArr.push('" style="');
        arcStrArr.push(curStyle);
        arcStrArr.push('">');
        arcStrArr.push(dec);
        if (arcCur) 
            arcStrArr.push('</a>');        
        else 
            arcStrArr.push('</span>');        
        return arcStrArr.join('');
    }
	
	this._generatePreLinkStr = function(page) {
		if (page.first)
			return "";
		else
			return this._generateLinkStr(true, this.norClass, this.norStyle, (page.curpage - 1), "\u4e0a\u4e00\u9875");
	}

	this._generateNextLinkStr = function(page) {
		if (page.last) 
			return "";
		else
			return this._generateLinkStr(true, this.norClass, this.norStyle, (page.curpage + 1), "\u4e0b\u4e00\u9875");
	}

	this._generateFirstLinkStr = function(page) {
		if (this.viewType == this.START || this.viewType == this.NONE)
			return "";
		else
			return this._generateLinkStr(true, this.norClass, this.norStyle, 1, this.decStr);
	}

	this._generateLastLinkStr = function(page) {
		if (this.viewType == this.END || this.viewType == this.NONE)
			return "";
		else
			return this._generateLinkStr(true, this.norClass, this.norStyle, page.totalPage, this.decStr);
	}

	this._generateMainLinkStr = function(page) {
		var linkStr = [];
		var index = this._getIndexAarry(page, this.maxSize);
		var length = index.length;
		for (var i = 0; i < length; i++) {
			if (index[i] == page.curpage)
				linkStr.push(this._generateLinkStr(this.arcCur, this.curClass, this.curStyle, page.curpage, this.decStr));
			else
				linkStr.push(this._generateLinkStr(true, this.norClass, this.norStyle, index[i], this.decStr));
			linkStr.push(" ");
		}
		return linkStr.join("");
	}
	
	this.generateTotalStr = function(page) {
		var totalPage = page.totalPage;
		if(totalPage == 1) {
			return;
		}
		var totalStr = [];
		var mainLink = this._generateMainLinkStr(page);
		var firstLink = this._generateFirstLinkStr(page);
		var preLink = this._generatePreLinkStr(page);
		var nextLink = this._generateNextLinkStr(page);
		var lastLink = this._generateLastLinkStr(page);
		totalStr.push(preLink);
		totalStr.push(" ");
		totalStr.push(firstLink);
		totalStr.push(" ");
		if (firstLink != "")
			totalStr.push("... ");
		totalStr.push(mainLink);
		totalStr.push(" ");
		if (lastLink != "")
			totalStr.push("... ");
		totalStr.push(lastLink);
		totalStr.push(" ");
		totalStr.push(nextLink);
		return totalStr.join("");
	}
}

PaginationUtils = {
	paginationJsGen:function(setting) {
			this.setting = {
				objId:"p",
				url:"",
				paramStr:"",
				firstFunc:"firstFunc",
				successFunc:"",
				errorFunc:"",
				beforeSendFunc:"",
				completeFunc:"",
				pageKey:"page",
				pageDivId:"",
				maxSize:"5",
				curClass:"",
				curStyle:"",
				normalClass:"",
				normalStyle:"",
				arcCur:"false",
				decStr:"%s"
			};
			jQuery.extend(this.setting, setting);
			var js = [this.setting.objId,"={pagination:new Pagination({",
			 "arcCur:",this.setting.arcCur,",",
			 "curClassStr:'",this.setting.curClass,"',",
			 "curStyleStr:'",this.setting.curStyle,"',",
			 "norClassStr:'",this.setting.normalClass,"',",
			 "norStyleStr:'",this.setting.normalStyle,"',",
			 "decStr:'",this.setting.decStr,"',",
			 "pageObj:'",this.setting.objId,"',",
			 "maxSize:",this.setting.maxSize,"}),",
			 "ajaxPageBack:function(data){",
			 "var page=data.",this.setting.pageKey,";",
			 "jQuery('#",this.setting.pageDivId,"').html(",this.setting.objId,".pagination.generateTotalStr(page));",
			 this.setting.successFunc,"(data);},",
			 this.setting.firstFunc,":function(){",
			 "return doAjax('",this.setting.url,"','",this.setting.paramStr,this.setting.paramStr?"&":"","curpage=1',",
			 this.setting.objId,".ajaxPageBack,",this.setting.errorFunc,",",this.setting.beforeSendFunc,",",this.setting.completeFunc,");},",
			 "jumpTo:function(i){",
			 "return doAjax('",this.setting.url,"','",this.setting.paramStr,this.setting.paramStr?"&":"","curpage='+i,",
			 this.setting.objId,".ajaxPageBack,",this.setting.errorFunc,",",this.setting.beforeSendFunc,",",this.setting.completeFunc,");}",
			 ",","}"].join("");
			eval(js);
		}	
};
		